
function getIndex(url,site_id) {
	uni.request({
		url: url + 'api/qianyu_smfw/settings/index',
		method: "POST",
		header: {
			'site-id': site_id //自定义请求头信息
		},
		data: {},
		success(res) {
			let result = res.data
			if (result.code == 1) {
				uni.setStorageSync('platform', result.data.platform)
				uni.setStorageSync('bom', result.data.bom)
				result.data.theme.ba_color = getLightColor(result.data.theme.main_color, 0.7)
				uni.setStorageSync('theme', result.data.theme)
				console.log('aseada', result.data.theme.ba_color)
			} else {
				uni.showToast({
					title: result.msg,
					//将值设置为 success 或者直接不用写icon这个参数
					icon: 'error',
					//显示持续时间为 2秒
					duration: 2000
				})
			}
		}
	})
}

function hexToRgb(str) {
	let hexs = null;
	let reg = /^\#?[0-9A-Fa-f]{6}$/;
	if (!reg.test(str)) return alert('色值不正确')
	str = str.replace('#', '') // 去掉#
	hexs = str.match(/../g) // 切割成数组 409EFF => ['40','9E','FF']
	// 将切割的色值转换为16进制
	for (let i = 0; i < hexs.length; i++) hexs[i] = parseInt(hexs[i], 16)
	return hexs // 返回rgb色值[64, 158, 255]
}

function getLightColor(color, level) {
	var reg = /^\#?[0-9A-Fa-f]{6}$/;
	if (!reg.test(color)) return alert('色值不正确')
	let rgb = hexToRgb(color);
	// 循环对色值进行调整
	for (let i = 0; i < 3; i++) {
		rgb[i] = Math.floor((255 - rgb[i]) * level + rgb[i]) // 始终保持在0-255之间
	}
	reg = /^\d{1,3}$/; // 限定1-3位 -> 0~255
	let hex = [rgb[0].toString(16), rgb[1].toString(16), rgb[2].toString(16)]
	// 转换的值如果只有一位则补0
	for (let i = 0; i < 3; i++) {
		if (hex[i].length === 1) hex[i] = `0${hex[i]}`
	}
	return `#${hex.join('')}`
}

export  {
	getIndex,
}